import 'package:flutter/widgets.dart';
import 'package:library_base/entities/enum_entity.dart';
import 'package:library_base/entities/in_order_page_goods_details_entity.dart';
import 'package:library_base/ext/double_ext.dart';
import 'package:library_base/provider/base_controller.dart';
import 'package:library_base/utils/num_util.dart';
import 'package:get/get.dart';
import 'package:library_base/widget/dialog/dialog_util.dart';
import 'package:library_base/ext/double_ext.dart';

class WmsSingleHeadEntryController
    extends BaseController {

  List<InOrderPageGoodsDetailsEntity> list = [];

  List<TextEditingController> weightControllers = [];

  List<FocusNode> weightFocusNodes = [];

  double totalWeight = 0.0;

  ///判断是否可以点击保存
  Rx<bool> isCanSave = false.obs;

  ///回调函数
  late Function(
      int position,
      List<InOrderPageGoodsDetailsEntity> listDetails,
      String locationId,
      String locationName,
      String? differenceReason
  ) updatePosition;

  int position = 0;

  late String stockInId;

  late String goodsCode;

  String? locationId;

  String? locationName;

  ///默认单位为kg，具体需要看服务器的单位，用于转换本地单位
  String serviceUnit = ServiceUnit.KG.name;

  @override
  void onCreate() {
    list = getArguments()['data'];
    position = getArguments()['index'];
    updatePosition = getArguments()['callback'];
    stockInId = getArguments()['stockInId'];
    goodsCode = getArguments()['goodsCode'];
    locationId = getArguments()['locationId'];
    locationName = getArguments()['locationName'];
    serviceUnit = getArguments()['serviceUnit'];

    for(int i = 0;i<list.length;i++){
      weightControllers.add(TextEditingController(text:list[i].stockInWeight!=0.0?list[i].stockInWeight.remoteUnitTransitionLocalUnitStr(serviceUnit):''));
      weightFocusNodes.add(FocusNode());
    }
    isCanSave.value = isCanClickDetermine();
    totalWeight = calculateTotalWeight();
  }

  ///重量变更
  void weightChange(String value) {
    isCanSave.value = isCanClickDetermine();
    totalWeight = calculateTotalWeight();
    update();
  }

  ///判断是否可以保存
  bool isCanClickDetermine() {
    for (int i = 0; i < weightControllers.length; i++) {
      var weightController = weightControllers[i];
      if (!NumUtil.isNumber(weightController.text)) {
        return false;
      } else {
        double weight = double.parse(weightController.text);
        if (weight == 0.0) {
          return false;
        }
      }
    }
    return true;
  }

  ///计算总重量
  double calculateTotalWeight() {
    double temporaryTotalWeight = 0.0;
    for (int i = 0; i < weightControllers.length; i++) {
      var weightController = weightControllers[i];
      if (NumUtil.isNumber(weightController.text)) {
        temporaryTotalWeight = NumUtil.add(temporaryTotalWeight, double.parse(weightController.text));
      }
    }
    return temporaryTotalWeight;
  }

  ///保存
  void save() {
    var arguments = {};
    arguments['id'] = stockInId;

    double totalWeight = 0.0;
    for (int i = 0; i < list.length; i++) {
      InOrderPageGoodsDetailsEntity detailsEntity = list[i];
      totalWeight = NumUtil.add(totalWeight, detailsEntity.stockInWeight);
    }
    arguments['goodsCode'] = goodsCode;
    arguments['stockInCount'] = list.length;
    arguments['stockInWeight'] = totalWeight.numAsUnit();
    arguments['locationId'] = locationId;
    arguments['locationName'] = locationName;
    arguments['updatePosition'] = updatePosition;
    arguments['position'] = position;
    for (int i = 0; i < weightControllers.length; i++) {
      var weightController = weightControllers[i];
      list[i].stockInWeight = double.parse(weightController.text);
    }
    arguments['goodsDetails'] = list;
    DialogUtil.showCreateWmsDialog(arguments: arguments);
  }

}
